﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class register_employee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] != null)
            Response.Redirect("main.aspx");

        Session["USERNAME"] = null;
        Session["PASSWORD"] = null;
        Session["IDENTITY"] = null;
    }
    protected void button1_Click(object sender, EventArgs e)
    {
        if (textbox5.Text.Equals(textbox6.Text) == false)
        {//密码不一致，返回
            Page.RegisterStartupScript("ss", "<script>alert('两次输入密码不一致，请重新输入！')</script>");
            return;
        }
        else
        {
            if (textbox5.Text.Length < 6 || textbox6.Text.Length > 8)
            {//密码长度不符合，返回
                Page.RegisterStartupScript("ss", "<script>alert('密码长度请保持在6~8位！')</script>");
                return;
            }
            else
            {
                //检验输入的企业名称是否存在，存在的话获取其ENTER_ID
                SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
                sc.Open();
                SqlCommand get_id = new SqlCommand("select ENTER_ID from INFO_ENTER where ENTER_NAME=@name",sc);
                get_id.Parameters.AddWithValue("@name",textbox4.Text);
                object a = get_id.ExecuteScalar();
                if (a == null)
                {//不存在，返回
                    Page.RegisterStartupScript("ss", "<script>alert('您输入的企业用户不存在！')</script>");
                    return;
                }
                else
                {//存在
                    string enterid = a.ToString();
                    //TO-DO: 检验输入的名称是否存在，存在的话返回
                    SqlCommand check_name = new SqlCommand("select STATUS from INFO_EMPLOYEE where EMPLOYEE_NAME=@namee",sc);
                    check_name.Parameters.AddWithValue("@namee",textbox1.Text);
                    object b = check_name.ExecuteScalar();
                    if (b != null)
                    {//存在，返回
                        Page.RegisterStartupScript("ss", "<script>alert('您要输入的销售角色名称已存在！')</script>");
                        return;
                    }
                    else
                    {
                        try
                        {
                            SqlCommand insert_new = new SqlCommand("insert into INFO_EMPLOYEE values (@ename,@ead,@ephone,@eid,0,0,@epsw)", sc);
                            insert_new.Parameters.AddWithValue("@ename", textbox1.Text);
                            insert_new.Parameters.AddWithValue("@ead", textbox2.Text);
                            insert_new.Parameters.AddWithValue("@ephone", textbox3.Text);
                            insert_new.Parameters.AddWithValue("@eid", enterid);
                            insert_new.Parameters.AddWithValue("@epsw", textbox5.Text);
                            while (Application["sqllock"] == "lock")
                            {//检测是否已经锁住               
                                System.Threading.Thread.Sleep(2000);
                            }
                            Application["sqllock"] = "lock";//上锁 
                            try
                            {
                                insert_new.ExecuteNonQuery();
                            }
                            finally
                            {
                                Application["sqllock"] = "unlock";//解锁 
                            }
                            Page.RegisterStartupScript("ss", "<script>alert('申请提交成功！')</script>");
                            return;
                        }
                        catch (Exception ex_sql)
                        {
                            Application["sqllock"] = "unlock";//解锁 
                            Page.RegisterStartupScript("ss", "<script>alert('数据库操作出现异常！"+ex_sql.Message+ex_sql.StackTrace+"')</script>");
                            return;
                        }
                        
                    }
                }
            }
        }
    }
}